{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "yb8VGi-S3HSu"
      },
      "source": [
        "**channel_last**"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "gwfncYrRsNvs",
        "outputId": "47bc1ba5-d7f0-46ba-b5d4-ea419e96d320"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Dataset: WHU_Hi_HanChuan\n",
            "Try using images from Data folder...\n",
            "+-------------------------------------+\n",
            "Input_mat shape: (1217, 303, 274)\n",
            "Total excution time...247.81842231750488seconds\n",
            "Total number of samples: 257309\n",
            "\n",
            "+------------Show Table---------------+\n",
            " Class#  Number of samples\n",
            "      1              44706\n",
            "      2              22687\n",
            "      3              10217\n",
            "      4               5333\n",
            "      5               1200\n",
            "      6               4504\n",
            "      7               5903\n",
            "      8              17978\n",
            "      9               9469\n",
            "     10              10516\n",
            "     11              16911\n",
            "     12               3679\n",
            "     13               9109\n",
            "     14              18560\n",
            "     15               1136\n",
            "     16              75401\n",
            "+-----------Close Table---------------+\n",
            "Class 1 is accepted\n",
            "Class 2 is accepted\n",
            "Class 3 is accepted\n",
            "Class 4 is accepted\n",
            "Class 5 is accepted\n",
            "Class 6 is accepted\n",
            "Class 7 is accepted\n",
            "Class 8 is accepted\n",
            "Class 9 is accepted\n",
            "Class 10 is accepted\n",
            "Class 11 is accepted\n",
            "Class 12 is accepted\n",
            "Class 13 is accepted\n",
            "Class 14 is accepted\n",
            "Class 15 is accepted\n",
            "Class 16 is accepted\n",
            "tcmalloc: large alloc 3754934272 bytes == 0x558d495fc000 @  0x7f2f4c0841e7 0x7f2f48dd60ce 0x7f2f48e32715 0x7f2f48e32d1b 0x7f2f48ed3333 0x55899d74934c 0x55899d749120 0x55899d7bd679 0x55899d7b802f 0x55899d7b7d43 0x55899d882302 0x55899d88267d 0x55899d882526 0x55899d85a1d3 0x55899d859e7c 0x7f2f4ae6ebf7 0x55899d859d5a\n",
            "+-------------------------------------+\n",
            "Size of Training data: 1600\n",
            "Size of Validation data: 800\n",
            "Size of Testing data: 69919\n",
            "+-------------------------------------+\n",
            "tcmalloc: large alloc 3754934272 bytes == 0x558e31678000 @  0x7f2f4c0841e7 0x7f2f48dd60ce 0x7f2f48e2ccf5 0x7f2f48e2ce08 0x7f2f48ebf0b9 0x7f2f48ec1a25 0x55899d832129 0x55899d7b9417 0x55899d7b802f 0x55899d7b7d43 0x55899d882302 0x55899d88267d 0x55899d882526 0x55899d85a1d3 0x55899d859e7c 0x7f2f4ae6ebf7 0x55899d859d5a\n",
            "tcmalloc: large alloc 3754934272 bytes == 0x558d4442c000 @  0x7f2f4c0841e7 0x55899d77b518 0x55899d745d17 0x7f2f48e22de8 0x7f2f48ec41b6 0x55899d749424 0x55899d749120 0x55899d7bdb80 0x55899d74a9da 0x55899d7b9108 0x55899d74a9da 0x55899d7b9108 0x55899d7b802f 0x55899d74aaba 0x55899d7b9108 0x55899d74a9da 0x55899d7b9108 0x55899d74a9da 0x55899d7b9108 0x55899d74a9da 0x55899d7b9108 0x55899d7b802f 0x55899d74aaba 0x55899d7b9108 0x55899d7b802f 0x55899d74aaba 0x55899d7bd2c0 0x55899d7b802f 0x55899d7b7d43 0x55899d882302 0x55899d88267d\n",
            "(800, 7, 7, 274)\n",
            "+-------------------------------------+\n",
            "Summary\n",
            "Train_patch.shape: (1600, 7, 7, 274)\n",
            "Train_label.shape: (1600, 16)\n",
            "Test_patch.shape: (69919, 7, 7, 274)\n",
            "Test_label.shape: (69919, 16)\n",
            "Validation batch Shape: (800, 7, 7, 274)\n",
            "Validation label Shape: (800, 16)\n",
            "+-------------------------------------+\n",
            "\n",
            "Finished processing.......\n"
          ]
        }
      ],
      "source": [
        "!python preprocess1.py"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ZQ2xveMigErG",
        "outputId": "dd0c7602-e1e9-44c4-b146-d70e48cb9b55"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Mounted at /content/drive\n"
          ]
        }
      ],
      "source": [
        "from google.colab import drive\n",
        "drive.mount('/content/drive')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "-jQOoSuYgNvZ",
        "outputId": "a4b8210a-38b3-4f29-fe4f-828ee6a27909"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "/content/drive/MyDrive/CNN-Based-Hyperspectral-Image-Classification-master\n"
          ]
        }
      ],
      "source": [
        "%cd /content/drive/MyDrive/CNN-Based-Hyperspectral-Image-Classification-master"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "9gNszXupmGT1",
        "outputId": "36005505-134d-479e-dbb8-196cdf4df0a5"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "TensorFlow 1.x selected.\n"
          ]
        }
      ],
      "source": [
        "%tensorflow_version 1.x"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "JvOdTts53NNN"
      },
      "source": [
        "**Data loading**"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 17
        },
        "id": "Cpy13VFvukXH",
        "outputId": "dbac9b06-da2d-401b-c14c-67f0622c4132"
      },
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/html": [
              "        <script type=\"text/javascript\">\n",
              "        window.PlotlyConfig = {MathJaxConfig: 'local'};\n",
              "        if (window.MathJax) {MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}\n",
              "        if (typeof require !== 'undefined') {\n",
              "        require.undef(\"plotly\");\n",
              "        requirejs.config({\n",
              "            paths: {\n",
              "                'plotly': ['https://cdn.plot.ly/plotly-2.8.3.min']\n",
              "            }\n",
              "        });\n",
              "        require(['plotly'], function(Plotly) {\n",
              "            window._Plotly = Plotly;\n",
              "        });\n",
              "        }\n",
              "        </script>\n",
              "        "
            ]
          },
          "metadata": {}
        }
      ],
      "source": [
        "import keras\n",
        "from keras.layers import Conv2D, Conv3D, Flatten, Dense, Reshape, BatchNormalization, Activation\n",
        "from keras.layers import Dropout, Input\n",
        "from keras.models import Model\n",
        "from keras.optimizers import adam_v2\n",
        "from keras.callbacks import ModelCheckpoint\n",
        "from keras.utils import np_utils\n",
        "\n",
        "from sklearn.decomposition import PCA\n",
        "from sklearn.model_selection import train_test_split\n",
        "from sklearn.metrics import confusion_matrix, accuracy_score, classification_report, cohen_kappa_score\n",
        "\n",
        "from operator import truediv\n",
        "\n",
        "from plotly.offline import init_notebook_mode\n",
        "\n",
        "import numpy as np\n",
        "import matplotlib.pyplot as plt\n",
        "import scipy.io as sio\n",
        "import os\n",
        "import spectral\n",
        "\n",
        "init_notebook_mode(connected=True)\n",
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "7_xZy999vMSp",
        "outputId": "160e3960-0fc1-4cf1-ccdc-55214d6e94d4"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Collecting spectral\n",
            "  Downloading spectral-0.22.4-py3-none-any.whl (212 kB)\n",
            "\u001b[?25l\r\u001b[K     |█▌                              | 10 kB 20.0 MB/s eta 0:00:01\r\u001b[K     |███                             | 20 kB 15.8 MB/s eta 0:00:01\r\u001b[K     |████▋                           | 30 kB 10.9 MB/s eta 0:00:01\r\u001b[K     |██████▏                         | 40 kB 9.4 MB/s eta 0:00:01\r\u001b[K     |███████▊                        | 51 kB 4.6 MB/s eta 0:00:01\r\u001b[K     |█████████▎                      | 61 kB 5.4 MB/s eta 0:00:01\r\u001b[K     |██████████▉                     | 71 kB 5.9 MB/s eta 0:00:01\r\u001b[K     |████████████▍                   | 81 kB 5.9 MB/s eta 0:00:01\r\u001b[K     |██████████████                  | 92 kB 6.5 MB/s eta 0:00:01\r\u001b[K     |███████████████▍                | 102 kB 5.2 MB/s eta 0:00:01\r\u001b[K     |█████████████████               | 112 kB 5.2 MB/s eta 0:00:01\r\u001b[K     |██████████████████▌             | 122 kB 5.2 MB/s eta 0:00:01\r\u001b[K     |████████████████████            | 133 kB 5.2 MB/s eta 0:00:01\r\u001b[K     |█████████████████████▋          | 143 kB 5.2 MB/s eta 0:00:01\r\u001b[K     |███████████████████████▏        | 153 kB 5.2 MB/s eta 0:00:01\r\u001b[K     |████████████████████████▊       | 163 kB 5.2 MB/s eta 0:00:01\r\u001b[K     |██████████████████████████▎     | 174 kB 5.2 MB/s eta 0:00:01\r\u001b[K     |███████████████████████████▉    | 184 kB 5.2 MB/s eta 0:00:01\r\u001b[K     |█████████████████████████████▎  | 194 kB 5.2 MB/s eta 0:00:01\r\u001b[K     |██████████████████████████████▉ | 204 kB 5.2 MB/s eta 0:00:01\r\u001b[K     |████████████████████████████████| 212 kB 5.2 MB/s \n",
            "\u001b[?25hRequirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from spectral) (1.21.5)\n",
            "Installing collected packages: spectral\n",
            "Successfully installed spectral-0.22.4\n"
          ]
        }
      ],
      "source": [
        "!pip install spectral"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
        "id": "l88iTw7_vU1l"
      },
      "outputs": [],
      "source": [
        "dataset = 'HC'\n",
        "test_ratio = 0.7\n",
        "windowSize = 25\n",
        "def loadData(name):\n",
        "    data_path = os.path.join(os.getcwd(),'data')\n",
        "    if name == 'IP':\n",
        "        data = sio.loadmat(os.path.join(data_path, 'Indian_pines_corrected.mat'))['indian_pines_corrected']\n",
        "        labels = sio.loadmat(os.path.join(data_path, 'Indian_pines_gt.mat'))['indian_pines_gt']\n",
        "    elif name == 'SA':\n",
        "        data = sio.loadmat(os.path.join(data_path, 'Salinas_corrected.mat'))['salinas_corrected']\n",
        "        labels = sio.loadmat(os.path.join(data_path, 'Salinas_gt.mat'))['salinas_gt']\n",
        "    elif name == 'HC':\n",
        "        data = sio.loadmat(os.path.join(data_path, 'WHU_Hi_HanChuan.mat'))['WHU_Hi_HanChuan']\n",
        "        labels = sio.loadmat(os.path.join(data_path, 'WHU_Hi_HanChuan_gt.mat'))['WHU_Hi_HanChuan_gt']\n",
        "    \n",
        "    return data, labels\n",
        "def splitTrainTestSet(X, y, testRatio, randomState=345):\n",
        "    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=testRatio, random_state=randomState,\n",
        "                                                        stratify=y)\n",
        "    return X_train, X_test, y_train, y_test\n",
        "def applyPCA(X1, numComponents=75):\n",
        "    newX = np.reshape(X1, (-1, X1.shape[3]))\n",
        "    pca = PCA(n_components=numComponents, whiten=True)\n",
        "    newX = pca.fit_transform(newX)\n",
        "    newX = np.reshape(newX, (X1.shape[0],X1.shape[1],X1.shape[2], numComponents))\n",
        "    return newX, pca\n",
        "def padWithZeros(X, margin=2):\n",
        "    newX = np.zeros((X.shape[0] + 2 * margin, X.shape[1] + 2* margin, X.shape[2]))\n",
        "    x_offset = margin\n",
        "    y_offset = margin\n",
        "    newX[x_offset:X.shape[0] + x_offset, y_offset:X.shape[1] + y_offset, :] = X\n",
        "    return newX\n",
        "def createImageCubes(X, y, windowSize=5, removeZeroLabels = True):\n",
        "    margin = int((windowSize - 1) / 2)\n",
        "    zeroPaddedX = padWithZeros(X, margin=margin)\n",
        "    # split patches\n",
        "    patchesData = np.zeros((X.shape[0] * X.shape[1], windowSize, windowSize, X.shape[2]))\n",
        "    patchesLabels = np.zeros((X.shape[0] * X.shape[1]))\n",
        "    patchIndex = 0\n",
        "    for r in range(margin, zeroPaddedX.shape[0] - margin):\n",
        "        for c in range(margin, zeroPaddedX.shape[1] - margin):\n",
        "            patch = zeroPaddedX[r - margin:r + margin + 1, c - margin:c + margin + 1]   \n",
        "            patchesData[patchIndex, :, :, :] = patch\n",
        "            patchesLabels[patchIndex] = y[r-margin, c-margin]\n",
        "            patchIndex = patchIndex + 1\n",
        "    if removeZeroLabels:\n",
        "        patchesData = patchesData[patchesLabels>0,:,:,:]\n",
        "        patchesLabels = patchesLabels[patchesLabels>0]\n",
        "        patchesLabels -= 1\n",
        "    return patchesData, patchesLabels"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {
        "id": "xNok-_LRqvzh"
      },
      "outputs": [],
      "source": [
        "def loadTestData(name):\n",
        "    data_path = os.path.join(os.getcwd(),'data')\n",
        "    data = sio.loadmat(os.path.join(data_path, 'WHU_Hi_HanChuan_Test_patch_7.mat'))[\"test_patch\"]\n",
        "    labels = sio.loadmat(os.path.join(data_path, 'WHU_Hi_HanChuan_Test_patch_7.mat'))[\"test_labels\"]\n",
        "    \n",
        "    return data, labels"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "HiKkREVevqo2",
        "outputId": "c8ccc985-83e1-4ae9-963d-d009836a2084"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "((69919, 7, 7, 274), (69919, 16))"
            ]
          },
          "metadata": {},
          "execution_count": 8
        }
      ],
      "source": [
        "X, y = loadTestData(dataset)\n",
        "\n",
        "X.shape, y.shape"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "metadata": {
        "id": "x4Yhz5HKx2bv"
      },
      "outputs": [],
      "source": [
        "def loadTrainData(name):\n",
        "    data_path = os.path.join(os.getcwd(),'data')\n",
        "    data = sio.loadmat(os.path.join(data_path, 'WHU_Hi_HanChuan_Train_patch_7.mat'))[\"train_patch\"]\n",
        "    labels = sio.loadmat(os.path.join(data_path, 'WHU_Hi_HanChuan_Train_patch_7.mat'))[\"train_labels\"]\n",
        "    \n",
        "    return data, labels\n",
        "\n",
        "def loadValData(name):\n",
        "    data_path = os.path.join(os.getcwd(),'data')\n",
        "    data = sio.loadmat(os.path.join(data_path, 'WHU_Hi_HanChuan_Val_patch_7.mat'))[\"val_patch\"]\n",
        "    labels = sio.loadmat(os.path.join(data_path, 'WHU_Hi_HanChuan_Val_patch_7.mat'))[\"val_labels\"]\n",
        "    \n",
        "    return data, labels"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "aWjNJ6vowXu8",
        "outputId": "ebbabc89-3682-40fe-f618-f345a067f8bd"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "((1600, 7, 7, 274),\n",
              " (69919, 7, 7, 274),\n",
              " (800, 7, 7, 274),\n",
              " (1600, 16),\n",
              " (69919, 16),\n",
              " (800, 16))"
            ]
          },
          "metadata": {},
          "execution_count": 10
        }
      ],
      "source": [
        "Xtest=X\n",
        "ytest=y\n",
        "Xtrain, ytrain = loadTrainData(dataset)\n",
        "Xval, yval = loadValData(dataset)\n",
        "\n",
        "Xtrain.shape, Xtest.shape, Xval.shape, ytrain.shape, ytest.shape, yval.shape"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "vvm3LY9i0SMe"
      },
      "source": [
        " **Model and Training**"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "\n",
        "\n",
        "**1.   Hybrid SN**\n",
        "\n",
        "\n"
      ],
      "metadata": {
        "id": "KbCgZRCpuMBk"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "VB_VSif70NpI",
        "outputId": "5e78e181-4905-4861-e498-7e387ad5239e"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(1600, 11, 11, 15, 1)"
            ]
          },
          "metadata": {},
          "execution_count": 15
        }
      ],
      "source": [
        "K = 15\n",
        "Xtrain = Xtrain.reshape(-1, 11, 11, K, 1)\n",
        "Xtrain.shape"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "JVpBY84L0lv-"
      },
      "outputs": [],
      "source": [
        "S = 11\n",
        "L = K\n",
        "output_units = 16"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "obBFI-nu05om",
        "outputId": "679ea0b5-bf83-43cc-95ff-6eae536a31b5"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "(None, 5, 5, 3, 32)\n"
          ]
        }
      ],
      "source": [
        "## input layer\n",
        "input_layer = Input((S, S, L, 1))\n",
        "\n",
        "## convolutional layers\n",
        "conv_layer1 = Conv3D(filters=8, kernel_size=(3, 3, 7), activation='relu')(input_layer)\n",
        "conv_layer2 = Conv3D(filters=16, kernel_size=(3, 3, 5), activation='relu')(conv_layer1)\n",
        "conv_layer3 = Conv3D(filters=32, kernel_size=(3, 3, 3), activation='relu')(conv_layer2)\n",
        "print(conv_layer3.shape)\n",
        "conv3d_shape = conv_layer3.shape\n",
        "conv_layer3 = Reshape((conv3d_shape[1], conv3d_shape[2], conv3d_shape[3]*conv3d_shape[4]))(conv_layer3)\n",
        "conv_layer4 = Conv2D(filters=128, kernel_size=(3,3), activation='relu')(conv_layer3)\n",
        "\n",
        "flatten_layer = Flatten()(conv_layer4)\n",
        "\n",
        "## fully connected layers\n",
        "dense_layer1 = Dense(units=128, activation='relu')(flatten_layer)\n",
        "dense_layer1 = Dropout(0.4)(dense_layer1)\n",
        "dense_layer2 = Dense(units=64, activation='relu')(dense_layer1)\n",
        "dense_layer2 = Dropout(0.4)(dense_layer2)\n",
        "output_layer = Dense(units=output_units, activation='softmax')(dense_layer2)"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "**2.   标准CNN**\n",
        "\n"
      ],
      "metadata": {
        "id": "iv-X05fvuAa0"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "S = 7\n",
        "L = Xtrain.shape[3]\n",
        "output_units = 16\n",
        "\n",
        "## input layer\n",
        "input_layer = Input((S, S, L))\n",
        "\n",
        "## convolutional layers\n",
        "#conv_layer1 = Conv2D(filters=128, kernel_size=(3,3), padding='same', activation='relu')(input_layer)\n",
        "#?bn_layer1 = BatchNormalization(conv_layer1, training='False')\n",
        "\n",
        "X = Conv2D(filters = 128, kernel_size = (3,3), padding = 'same') (input_layer)\n",
        "X = BatchNormalization()(X)\n",
        "X = Activation('relu')(X)\n",
        "\n",
        "X = Conv2D(filters = 256, kernel_size = (3,3), padding = 'valid') (X)\n",
        "X = BatchNormalization()(X)\n",
        "X = Activation('relu')(X)\n",
        "\n",
        "X = Conv2D(filters = 256, kernel_size = (3,3), padding = 'valid') (X)\n",
        "X = BatchNormalization()(X)\n",
        "X = Activation('relu')(X)\n",
        "\n",
        "X = Conv2D(filters = 128, kernel_size = (3,3), padding = 'valid') (X)\n",
        "X = BatchNormalization()(X)\n",
        "X = Activation('relu')(X)\n",
        "\n",
        "## flatten\n",
        "flatten_layer = Flatten()(X)\n",
        "\n",
        "## fully connected layers\n",
        "dense_layer1 = Dense(units=128, activation='relu')(flatten_layer)\n",
        "dense_layer1 = Dropout(0.5)(dense_layer1)\n",
        "dense_layer2 = Dense(units=64, activation='relu')(dense_layer1)\n",
        "dense_layer2 = Dropout(0.5)(dense_layer2)\n",
        "output_layer = Dense(units=output_units, activation='softmax')(dense_layer2)"
      ],
      "metadata": {
        "id": "Ov1FYaRZJZ0E"
      },
      "execution_count": 30,
      "outputs": []
    },
    {
      "cell_type": "code",
      "execution_count": 31,
      "metadata": {
        "id": "eso6-eXU1gvO"
      },
      "outputs": [],
      "source": [
        "model = Model(inputs=input_layer, outputs=output_layer)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 32,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "DkcQalFA1tot",
        "outputId": "80e0c3c9-1bda-4c92-8340-18e614d2e2ed"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Model: \"model_2\"\n",
            "_________________________________________________________________\n",
            " Layer (type)                Output Shape              Param #   \n",
            "=================================================================\n",
            " input_5 (InputLayer)        [(None, 7, 7, 274)]       0         \n",
            "                                                                 \n",
            " conv2d_15 (Conv2D)          (None, 7, 7, 128)         315776    \n",
            "                                                                 \n",
            " batch_normalization_14 (Bat  (None, 7, 7, 128)        512       \n",
            " chNormalization)                                                \n",
            "                                                                 \n",
            " activation_14 (Activation)  (None, 7, 7, 128)         0         \n",
            "                                                                 \n",
            " conv2d_16 (Conv2D)          (None, 5, 5, 256)         295168    \n",
            "                                                                 \n",
            " batch_normalization_15 (Bat  (None, 5, 5, 256)        1024      \n",
            " chNormalization)                                                \n",
            "                                                                 \n",
            " activation_15 (Activation)  (None, 5, 5, 256)         0         \n",
            "                                                                 \n",
            " conv2d_17 (Conv2D)          (None, 3, 3, 256)         590080    \n",
            "                                                                 \n",
            " batch_normalization_16 (Bat  (None, 3, 3, 256)        1024      \n",
            " chNormalization)                                                \n",
            "                                                                 \n",
            " activation_16 (Activation)  (None, 3, 3, 256)         0         \n",
            "                                                                 \n",
            " conv2d_18 (Conv2D)          (None, 1, 1, 128)         295040    \n",
            "                                                                 \n",
            " batch_normalization_17 (Bat  (None, 1, 1, 128)        512       \n",
            " chNormalization)                                                \n",
            "                                                                 \n",
            " activation_17 (Activation)  (None, 1, 1, 128)         0         \n",
            "                                                                 \n",
            " flatten_3 (Flatten)         (None, 128)               0         \n",
            "                                                                 \n",
            " dense_9 (Dense)             (None, 128)               16512     \n",
            "                                                                 \n",
            " dropout_9 (Dropout)         (None, 128)               0         \n",
            "                                                                 \n",
            " dense_10 (Dense)            (None, 64)                8256      \n",
            "                                                                 \n",
            " dropout_10 (Dropout)        (None, 64)                0         \n",
            "                                                                 \n",
            " dense_11 (Dense)            (None, 16)                1040      \n",
            "                                                                 \n",
            "=================================================================\n",
            "Total params: 1,524,944\n",
            "Trainable params: 1,523,408\n",
            "Non-trainable params: 1,536\n",
            "_________________________________________________________________\n"
          ]
        }
      ],
      "source": [
        "model.summary()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 33,
      "metadata": {
        "id": "Qm_QgBwZ1wiZ"
      },
      "outputs": [],
      "source": [
        "# compiling the model\n",
        "adam = adam_v2.Adam(learning_rate=0.001, decay=1e-06)\n",
        "model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 34,
      "metadata": {
        "id": "fzY7O4_S12eW"
      },
      "outputs": [],
      "source": [
        "# checkpoint\n",
        "filepath = \"best-model.hdf5\"\n",
        "checkpoint = ModelCheckpoint(filepath, monitor='accuracy', verbose=1, save_best_only=True, mode='max')\n",
        "callbacks_list = [checkpoint]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 35,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "HbdA2F6Q15eO",
        "outputId": "3d2c0b08-832e-4f14-8a65-2a88ada4bb3b"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Epoch 1/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 2.7884 - accuracy: 0.1256\n",
            "Epoch 1: accuracy improved from -inf to 0.12562, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 15s 1s/step - loss: 2.7884 - accuracy: 0.1256 - val_loss: 3.0754 - val_accuracy: 0.0637\n",
            "Epoch 2/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 2.4759 - accuracy: 0.1925\n",
            "Epoch 2: accuracy improved from 0.12562 to 0.19250, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 7s 1s/step - loss: 2.4759 - accuracy: 0.1925 - val_loss: 3.9310 - val_accuracy: 0.0675\n",
            "Epoch 3/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 2.3739 - accuracy: 0.2338\n",
            "Epoch 3: accuracy improved from 0.19250 to 0.23375, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 7s 986ms/step - loss: 2.3739 - accuracy: 0.2338 - val_loss: 3.3470 - val_accuracy: 0.0725\n",
            "Epoch 4/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 2.2583 - accuracy: 0.2463\n",
            "Epoch 4: accuracy improved from 0.23375 to 0.24625, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 8s 1s/step - loss: 2.2583 - accuracy: 0.2463 - val_loss: 3.6197 - val_accuracy: 0.1375\n",
            "Epoch 5/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 2.1650 - accuracy: 0.2744\n",
            "Epoch 5: accuracy improved from 0.24625 to 0.27437, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 9s 1s/step - loss: 2.1650 - accuracy: 0.2744 - val_loss: 3.3746 - val_accuracy: 0.1475\n",
            "Epoch 6/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 2.0684 - accuracy: 0.3150\n",
            "Epoch 6: accuracy improved from 0.27437 to 0.31500, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 6s 914ms/step - loss: 2.0684 - accuracy: 0.3150 - val_loss: 2.9245 - val_accuracy: 0.1725\n",
            "Epoch 7/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 1.9594 - accuracy: 0.3531\n",
            "Epoch 7: accuracy improved from 0.31500 to 0.35313, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 6s 896ms/step - loss: 1.9594 - accuracy: 0.3531 - val_loss: 3.1128 - val_accuracy: 0.2200\n",
            "Epoch 8/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 1.8658 - accuracy: 0.3650\n",
            "Epoch 8: accuracy improved from 0.35313 to 0.36500, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 6s 901ms/step - loss: 1.8658 - accuracy: 0.3650 - val_loss: 3.0877 - val_accuracy: 0.2850\n",
            "Epoch 9/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 1.8492 - accuracy: 0.3669\n",
            "Epoch 9: accuracy improved from 0.36500 to 0.36687, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 19s 3s/step - loss: 1.8492 - accuracy: 0.3669 - val_loss: 2.4808 - val_accuracy: 0.3063\n",
            "Epoch 10/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 1.8081 - accuracy: 0.3806\n",
            "Epoch 10: accuracy improved from 0.36687 to 0.38063, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 7s 1s/step - loss: 1.8081 - accuracy: 0.3806 - val_loss: 3.2032 - val_accuracy: 0.2575\n",
            "Epoch 11/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 1.7701 - accuracy: 0.3781\n",
            "Epoch 11: accuracy did not improve from 0.38063\n",
            "7/7 [==============================] - 6s 867ms/step - loss: 1.7701 - accuracy: 0.3781 - val_loss: 3.4921 - val_accuracy: 0.3137\n",
            "Epoch 12/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 1.6997 - accuracy: 0.4156\n",
            "Epoch 12: accuracy improved from 0.38063 to 0.41563, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 9s 1s/step - loss: 1.6997 - accuracy: 0.4156 - val_loss: 3.4936 - val_accuracy: 0.2750\n",
            "Epoch 13/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 1.6318 - accuracy: 0.4313\n",
            "Epoch 13: accuracy improved from 0.41563 to 0.43125, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 8s 1s/step - loss: 1.6318 - accuracy: 0.4313 - val_loss: 2.9519 - val_accuracy: 0.3462\n",
            "Epoch 14/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 1.6070 - accuracy: 0.4500\n",
            "Epoch 14: accuracy improved from 0.43125 to 0.45000, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 6s 887ms/step - loss: 1.6070 - accuracy: 0.4500 - val_loss: 2.8858 - val_accuracy: 0.3825\n",
            "Epoch 15/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 1.5139 - accuracy: 0.4831\n",
            "Epoch 15: accuracy improved from 0.45000 to 0.48313, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 6s 889ms/step - loss: 1.5139 - accuracy: 0.4831 - val_loss: 2.3227 - val_accuracy: 0.3663\n",
            "Epoch 16/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 1.4738 - accuracy: 0.4894\n",
            "Epoch 16: accuracy improved from 0.48313 to 0.48937, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 14s 2s/step - loss: 1.4738 - accuracy: 0.4894 - val_loss: 2.1230 - val_accuracy: 0.4538\n",
            "Epoch 17/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 1.4918 - accuracy: 0.4950\n",
            "Epoch 17: accuracy improved from 0.48937 to 0.49500, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 9s 1s/step - loss: 1.4918 - accuracy: 0.4950 - val_loss: 2.1467 - val_accuracy: 0.3575\n",
            "Epoch 18/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 1.4678 - accuracy: 0.4969\n",
            "Epoch 18: accuracy improved from 0.49500 to 0.49687, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 13s 2s/step - loss: 1.4678 - accuracy: 0.4969 - val_loss: 1.9471 - val_accuracy: 0.4025\n",
            "Epoch 19/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 1.4229 - accuracy: 0.4975\n",
            "Epoch 19: accuracy improved from 0.49687 to 0.49750, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 8s 1s/step - loss: 1.4229 - accuracy: 0.4975 - val_loss: 2.3621 - val_accuracy: 0.3887\n",
            "Epoch 20/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 1.4052 - accuracy: 0.5144\n",
            "Epoch 20: accuracy improved from 0.49750 to 0.51437, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 9s 1s/step - loss: 1.4052 - accuracy: 0.5144 - val_loss: 3.7690 - val_accuracy: 0.3663\n",
            "Epoch 21/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 1.3674 - accuracy: 0.5225\n",
            "Epoch 21: accuracy improved from 0.51437 to 0.52250, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 6s 903ms/step - loss: 1.3674 - accuracy: 0.5225 - val_loss: 4.4911 - val_accuracy: 0.3462\n",
            "Epoch 22/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 1.3256 - accuracy: 0.5281\n",
            "Epoch 22: accuracy improved from 0.52250 to 0.52812, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 6s 906ms/step - loss: 1.3256 - accuracy: 0.5281 - val_loss: 3.3582 - val_accuracy: 0.3575\n",
            "Epoch 23/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 1.3012 - accuracy: 0.5375\n",
            "Epoch 23: accuracy improved from 0.52812 to 0.53750, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 6s 894ms/step - loss: 1.3012 - accuracy: 0.5375 - val_loss: 2.9359 - val_accuracy: 0.3875\n",
            "Epoch 24/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 1.2280 - accuracy: 0.5781\n",
            "Epoch 24: accuracy improved from 0.53750 to 0.57812, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 6s 906ms/step - loss: 1.2280 - accuracy: 0.5781 - val_loss: 2.2796 - val_accuracy: 0.4338\n",
            "Epoch 25/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 1.2573 - accuracy: 0.5663\n",
            "Epoch 25: accuracy did not improve from 0.57812\n",
            "7/7 [==============================] - 8s 1s/step - loss: 1.2573 - accuracy: 0.5663 - val_loss: 2.6438 - val_accuracy: 0.4025\n",
            "Epoch 26/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 1.1934 - accuracy: 0.5781\n",
            "Epoch 26: accuracy did not improve from 0.57812\n",
            "7/7 [==============================] - 6s 876ms/step - loss: 1.1934 - accuracy: 0.5781 - val_loss: 2.0778 - val_accuracy: 0.4475\n",
            "Epoch 27/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 1.0979 - accuracy: 0.6256\n",
            "Epoch 27: accuracy improved from 0.57812 to 0.62563, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 7s 996ms/step - loss: 1.0979 - accuracy: 0.6256 - val_loss: 2.1513 - val_accuracy: 0.4800\n",
            "Epoch 28/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 1.1355 - accuracy: 0.5819\n",
            "Epoch 28: accuracy did not improve from 0.62563\n",
            "7/7 [==============================] - 6s 860ms/step - loss: 1.1355 - accuracy: 0.5819 - val_loss: 2.1592 - val_accuracy: 0.4400\n",
            "Epoch 29/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 1.1421 - accuracy: 0.6125\n",
            "Epoch 29: accuracy did not improve from 0.62563\n",
            "7/7 [==============================] - 7s 979ms/step - loss: 1.1421 - accuracy: 0.6125 - val_loss: 2.7718 - val_accuracy: 0.4363\n",
            "Epoch 30/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 1.1338 - accuracy: 0.6112\n",
            "Epoch 30: accuracy did not improve from 0.62563\n",
            "7/7 [==============================] - 6s 850ms/step - loss: 1.1338 - accuracy: 0.6112 - val_loss: 1.8381 - val_accuracy: 0.5450\n",
            "Epoch 31/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 1.0267 - accuracy: 0.6319\n",
            "Epoch 31: accuracy improved from 0.62563 to 0.63187, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 6s 932ms/step - loss: 1.0267 - accuracy: 0.6319 - val_loss: 2.0253 - val_accuracy: 0.5125\n",
            "Epoch 32/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.9828 - accuracy: 0.6506\n",
            "Epoch 32: accuracy improved from 0.63187 to 0.65062, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 13s 2s/step - loss: 0.9828 - accuracy: 0.6506 - val_loss: 1.8533 - val_accuracy: 0.4938\n",
            "Epoch 33/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.9914 - accuracy: 0.6562\n",
            "Epoch 33: accuracy improved from 0.65062 to 0.65625, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 6s 864ms/step - loss: 0.9914 - accuracy: 0.6562 - val_loss: 1.6751 - val_accuracy: 0.5325\n",
            "Epoch 34/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.9747 - accuracy: 0.6681\n",
            "Epoch 34: accuracy improved from 0.65625 to 0.66812, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 6s 911ms/step - loss: 0.9747 - accuracy: 0.6681 - val_loss: 1.5292 - val_accuracy: 0.5863\n",
            "Epoch 35/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.9853 - accuracy: 0.6644\n",
            "Epoch 35: accuracy did not improve from 0.66812\n",
            "7/7 [==============================] - 6s 880ms/step - loss: 0.9853 - accuracy: 0.6644 - val_loss: 1.8287 - val_accuracy: 0.4900\n",
            "Epoch 36/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.9712 - accuracy: 0.6775\n",
            "Epoch 36: accuracy improved from 0.66812 to 0.67750, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 6s 918ms/step - loss: 0.9712 - accuracy: 0.6775 - val_loss: 2.3290 - val_accuracy: 0.4400\n",
            "Epoch 37/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 1.0029 - accuracy: 0.6513\n",
            "Epoch 37: accuracy did not improve from 0.67750\n",
            "7/7 [==============================] - 6s 837ms/step - loss: 1.0029 - accuracy: 0.6513 - val_loss: 1.8776 - val_accuracy: 0.5400\n",
            "Epoch 38/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.9075 - accuracy: 0.6888\n",
            "Epoch 38: accuracy improved from 0.67750 to 0.68875, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 6s 923ms/step - loss: 0.9075 - accuracy: 0.6888 - val_loss: 1.6198 - val_accuracy: 0.4988\n",
            "Epoch 39/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.9011 - accuracy: 0.6994\n",
            "Epoch 39: accuracy improved from 0.68875 to 0.69937, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 6s 873ms/step - loss: 0.9011 - accuracy: 0.6994 - val_loss: 1.5932 - val_accuracy: 0.5362\n",
            "Epoch 40/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.8271 - accuracy: 0.7194\n",
            "Epoch 40: accuracy improved from 0.69937 to 0.71938, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 6s 868ms/step - loss: 0.8271 - accuracy: 0.7194 - val_loss: 1.4384 - val_accuracy: 0.5900\n",
            "Epoch 41/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.8225 - accuracy: 0.7194\n",
            "Epoch 41: accuracy did not improve from 0.71938\n",
            "7/7 [==============================] - 6s 852ms/step - loss: 0.8225 - accuracy: 0.7194 - val_loss: 1.4639 - val_accuracy: 0.5888\n",
            "Epoch 42/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.8624 - accuracy: 0.7144\n",
            "Epoch 42: accuracy did not improve from 0.71938\n",
            "7/7 [==============================] - 6s 871ms/step - loss: 0.8624 - accuracy: 0.7144 - val_loss: 1.6668 - val_accuracy: 0.5312\n",
            "Epoch 43/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.8575 - accuracy: 0.7075\n",
            "Epoch 43: accuracy did not improve from 0.71938\n",
            "7/7 [==============================] - 6s 838ms/step - loss: 0.8575 - accuracy: 0.7075 - val_loss: 1.2019 - val_accuracy: 0.6388\n",
            "Epoch 44/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.8410 - accuracy: 0.7119\n",
            "Epoch 44: accuracy did not improve from 0.71938\n",
            "7/7 [==============================] - 6s 849ms/step - loss: 0.8410 - accuracy: 0.7119 - val_loss: 1.3589 - val_accuracy: 0.5550\n",
            "Epoch 45/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.7657 - accuracy: 0.7500\n",
            "Epoch 45: accuracy improved from 0.71938 to 0.75000, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 7s 1s/step - loss: 0.7657 - accuracy: 0.7500 - val_loss: 1.0594 - val_accuracy: 0.6600\n",
            "Epoch 46/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.8154 - accuracy: 0.7437\n",
            "Epoch 46: accuracy did not improve from 0.75000\n",
            "7/7 [==============================] - 6s 856ms/step - loss: 0.8154 - accuracy: 0.7437 - val_loss: 1.9790 - val_accuracy: 0.6225\n",
            "Epoch 47/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.7891 - accuracy: 0.7425\n",
            "Epoch 47: accuracy did not improve from 0.75000\n",
            "7/7 [==============================] - 6s 850ms/step - loss: 0.7891 - accuracy: 0.7425 - val_loss: 1.6078 - val_accuracy: 0.5900\n",
            "Epoch 48/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.7964 - accuracy: 0.7356\n",
            "Epoch 48: accuracy did not improve from 0.75000\n",
            "7/7 [==============================] - 6s 851ms/step - loss: 0.7964 - accuracy: 0.7356 - val_loss: 1.4843 - val_accuracy: 0.6712\n",
            "Epoch 49/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.7815 - accuracy: 0.7419\n",
            "Epoch 49: accuracy did not improve from 0.75000\n",
            "7/7 [==============================] - 6s 867ms/step - loss: 0.7815 - accuracy: 0.7419 - val_loss: 1.4989 - val_accuracy: 0.6000\n",
            "Epoch 50/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.7703 - accuracy: 0.7437\n",
            "Epoch 50: accuracy did not improve from 0.75000\n",
            "7/7 [==============================] - 6s 850ms/step - loss: 0.7703 - accuracy: 0.7437 - val_loss: 1.3419 - val_accuracy: 0.6200\n",
            "Epoch 51/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.7498 - accuracy: 0.7431\n",
            "Epoch 51: accuracy did not improve from 0.75000\n",
            "7/7 [==============================] - 6s 858ms/step - loss: 0.7498 - accuracy: 0.7431 - val_loss: 1.9527 - val_accuracy: 0.5337\n",
            "Epoch 52/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.7856 - accuracy: 0.7450\n",
            "Epoch 52: accuracy did not improve from 0.75000\n",
            "7/7 [==============================] - 6s 856ms/step - loss: 0.7856 - accuracy: 0.7450 - val_loss: 1.9096 - val_accuracy: 0.6037\n",
            "Epoch 53/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.7229 - accuracy: 0.7606\n",
            "Epoch 53: accuracy improved from 0.75000 to 0.76063, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 6s 936ms/step - loss: 0.7229 - accuracy: 0.7606 - val_loss: 1.3242 - val_accuracy: 0.6175\n",
            "Epoch 54/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.7262 - accuracy: 0.7588\n",
            "Epoch 54: accuracy did not improve from 0.76063\n",
            "7/7 [==============================] - 6s 860ms/step - loss: 0.7262 - accuracy: 0.7588 - val_loss: 1.7292 - val_accuracy: 0.6100\n",
            "Epoch 55/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.6715 - accuracy: 0.7725\n",
            "Epoch 55: accuracy improved from 0.76063 to 0.77250, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 7s 949ms/step - loss: 0.6715 - accuracy: 0.7725 - val_loss: 1.5239 - val_accuracy: 0.6288\n",
            "Epoch 56/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.6238 - accuracy: 0.7919\n",
            "Epoch 56: accuracy improved from 0.77250 to 0.79188, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 6s 889ms/step - loss: 0.6238 - accuracy: 0.7919 - val_loss: 1.9369 - val_accuracy: 0.6162\n",
            "Epoch 57/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.6568 - accuracy: 0.7831\n",
            "Epoch 57: accuracy did not improve from 0.79188\n",
            "7/7 [==============================] - 6s 868ms/step - loss: 0.6568 - accuracy: 0.7831 - val_loss: 0.9657 - val_accuracy: 0.7013\n",
            "Epoch 58/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.6751 - accuracy: 0.7944\n",
            "Epoch 58: accuracy improved from 0.79188 to 0.79438, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 7s 944ms/step - loss: 0.6751 - accuracy: 0.7944 - val_loss: 1.8941 - val_accuracy: 0.6225\n",
            "Epoch 59/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.6737 - accuracy: 0.7862\n",
            "Epoch 59: accuracy did not improve from 0.79438\n",
            "7/7 [==============================] - 6s 881ms/step - loss: 0.6737 - accuracy: 0.7862 - val_loss: 1.6659 - val_accuracy: 0.6125\n",
            "Epoch 60/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.6751 - accuracy: 0.7756\n",
            "Epoch 60: accuracy did not improve from 0.79438\n",
            "7/7 [==============================] - 7s 997ms/step - loss: 0.6751 - accuracy: 0.7756 - val_loss: 1.5005 - val_accuracy: 0.6137\n",
            "Epoch 61/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.6279 - accuracy: 0.7981\n",
            "Epoch 61: accuracy improved from 0.79438 to 0.79813, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 7s 954ms/step - loss: 0.6279 - accuracy: 0.7981 - val_loss: 1.3140 - val_accuracy: 0.6275\n",
            "Epoch 62/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.6236 - accuracy: 0.7912\n",
            "Epoch 62: accuracy did not improve from 0.79813\n",
            "7/7 [==============================] - 6s 838ms/step - loss: 0.6236 - accuracy: 0.7912 - val_loss: 2.0415 - val_accuracy: 0.6450\n",
            "Epoch 63/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.5920 - accuracy: 0.8000\n",
            "Epoch 63: accuracy improved from 0.79813 to 0.80000, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 7s 966ms/step - loss: 0.5920 - accuracy: 0.8000 - val_loss: 2.3190 - val_accuracy: 0.5975\n",
            "Epoch 64/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.5808 - accuracy: 0.8181\n",
            "Epoch 64: accuracy improved from 0.80000 to 0.81813, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 7s 965ms/step - loss: 0.5808 - accuracy: 0.8181 - val_loss: 1.5884 - val_accuracy: 0.6025\n",
            "Epoch 65/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.6379 - accuracy: 0.7931\n",
            "Epoch 65: accuracy did not improve from 0.81813\n",
            "7/7 [==============================] - 6s 844ms/step - loss: 0.6379 - accuracy: 0.7931 - val_loss: 2.0426 - val_accuracy: 0.6587\n",
            "Epoch 66/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.5813 - accuracy: 0.8100\n",
            "Epoch 66: accuracy did not improve from 0.81813\n",
            "7/7 [==============================] - 6s 863ms/step - loss: 0.5813 - accuracy: 0.8100 - val_loss: 1.0292 - val_accuracy: 0.7337\n",
            "Epoch 67/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.5638 - accuracy: 0.8175\n",
            "Epoch 67: accuracy did not improve from 0.81813\n",
            "7/7 [==============================] - 6s 845ms/step - loss: 0.5638 - accuracy: 0.8175 - val_loss: 0.6557 - val_accuracy: 0.7962\n",
            "Epoch 68/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.5823 - accuracy: 0.8194\n",
            "Epoch 68: accuracy improved from 0.81813 to 0.81937, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 6s 935ms/step - loss: 0.5823 - accuracy: 0.8194 - val_loss: 0.7517 - val_accuracy: 0.7725\n",
            "Epoch 69/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.6733 - accuracy: 0.7819\n",
            "Epoch 69: accuracy did not improve from 0.81937\n",
            "7/7 [==============================] - 6s 839ms/step - loss: 0.6733 - accuracy: 0.7819 - val_loss: 1.3835 - val_accuracy: 0.6425\n",
            "Epoch 70/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.6381 - accuracy: 0.7944\n",
            "Epoch 70: accuracy did not improve from 0.81937\n",
            "7/7 [==============================] - 7s 923ms/step - loss: 0.6381 - accuracy: 0.7944 - val_loss: 1.8450 - val_accuracy: 0.5537\n",
            "Epoch 71/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.6897 - accuracy: 0.7919\n",
            "Epoch 71: accuracy did not improve from 0.81937\n",
            "7/7 [==============================] - 6s 870ms/step - loss: 0.6897 - accuracy: 0.7919 - val_loss: 2.0340 - val_accuracy: 0.5337\n",
            "Epoch 72/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.5904 - accuracy: 0.8125\n",
            "Epoch 72: accuracy did not improve from 0.81937\n",
            "7/7 [==============================] - 6s 867ms/step - loss: 0.5904 - accuracy: 0.8125 - val_loss: 2.0591 - val_accuracy: 0.5325\n",
            "Epoch 73/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.5528 - accuracy: 0.8188\n",
            "Epoch 73: accuracy did not improve from 0.81937\n",
            "7/7 [==============================] - 6s 866ms/step - loss: 0.5528 - accuracy: 0.8188 - val_loss: 2.5352 - val_accuracy: 0.5600\n",
            "Epoch 74/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.5657 - accuracy: 0.8181\n",
            "Epoch 74: accuracy did not improve from 0.81937\n",
            "7/7 [==============================] - 6s 924ms/step - loss: 0.5657 - accuracy: 0.8181 - val_loss: 1.8187 - val_accuracy: 0.6250\n",
            "Epoch 75/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.6710 - accuracy: 0.7975\n",
            "Epoch 75: accuracy did not improve from 0.81937\n",
            "7/7 [==============================] - 6s 892ms/step - loss: 0.6710 - accuracy: 0.7975 - val_loss: 1.1831 - val_accuracy: 0.6913\n",
            "Epoch 76/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.5731 - accuracy: 0.8081\n",
            "Epoch 76: accuracy did not improve from 0.81937\n",
            "7/7 [==============================] - 6s 892ms/step - loss: 0.5731 - accuracy: 0.8081 - val_loss: 1.3480 - val_accuracy: 0.6237\n",
            "Epoch 77/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.5451 - accuracy: 0.8087\n",
            "Epoch 77: accuracy did not improve from 0.81937\n",
            "7/7 [==============================] - 6s 884ms/step - loss: 0.5451 - accuracy: 0.8087 - val_loss: 1.0920 - val_accuracy: 0.6812\n",
            "Epoch 78/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.5167 - accuracy: 0.8338\n",
            "Epoch 78: accuracy improved from 0.81937 to 0.83375, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 7s 1s/step - loss: 0.5167 - accuracy: 0.8338 - val_loss: 0.9739 - val_accuracy: 0.6988\n",
            "Epoch 79/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.5483 - accuracy: 0.8313\n",
            "Epoch 79: accuracy did not improve from 0.83375\n",
            "7/7 [==============================] - 6s 884ms/step - loss: 0.5483 - accuracy: 0.8313 - val_loss: 0.8760 - val_accuracy: 0.7337\n",
            "Epoch 80/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.5322 - accuracy: 0.8338\n",
            "Epoch 80: accuracy did not improve from 0.83375\n",
            "7/7 [==============================] - 6s 874ms/step - loss: 0.5322 - accuracy: 0.8338 - val_loss: 0.9893 - val_accuracy: 0.6812\n",
            "Epoch 81/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.5275 - accuracy: 0.8269\n",
            "Epoch 81: accuracy did not improve from 0.83375\n",
            "7/7 [==============================] - 6s 874ms/step - loss: 0.5275 - accuracy: 0.8269 - val_loss: 1.3735 - val_accuracy: 0.6237\n",
            "Epoch 82/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.5353 - accuracy: 0.8469\n",
            "Epoch 82: accuracy improved from 0.83375 to 0.84688, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 6s 934ms/step - loss: 0.5353 - accuracy: 0.8469 - val_loss: 1.2885 - val_accuracy: 0.7175\n",
            "Epoch 83/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.4345 - accuracy: 0.8594\n",
            "Epoch 83: accuracy improved from 0.84688 to 0.85938, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 7s 974ms/step - loss: 0.4345 - accuracy: 0.8594 - val_loss: 0.7156 - val_accuracy: 0.7887\n",
            "Epoch 84/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.4688 - accuracy: 0.8537\n",
            "Epoch 84: accuracy did not improve from 0.85938\n",
            "7/7 [==============================] - 7s 1s/step - loss: 0.4688 - accuracy: 0.8537 - val_loss: 1.1214 - val_accuracy: 0.6888\n",
            "Epoch 85/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.4635 - accuracy: 0.8544\n",
            "Epoch 85: accuracy did not improve from 0.85938\n",
            "7/7 [==============================] - 6s 857ms/step - loss: 0.4635 - accuracy: 0.8544 - val_loss: 1.2796 - val_accuracy: 0.6988\n",
            "Epoch 86/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.5561 - accuracy: 0.8194\n",
            "Epoch 86: accuracy did not improve from 0.85938\n",
            "7/7 [==============================] - 6s 836ms/step - loss: 0.5561 - accuracy: 0.8194 - val_loss: 0.6826 - val_accuracy: 0.7962\n",
            "Epoch 87/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.5620 - accuracy: 0.8263\n",
            "Epoch 87: accuracy did not improve from 0.85938\n",
            "7/7 [==============================] - 6s 853ms/step - loss: 0.5620 - accuracy: 0.8263 - val_loss: 3.0454 - val_accuracy: 0.6062\n",
            "Epoch 88/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.5553 - accuracy: 0.8225\n",
            "Epoch 88: accuracy did not improve from 0.85938\n",
            "7/7 [==============================] - 7s 936ms/step - loss: 0.5553 - accuracy: 0.8225 - val_loss: 1.8364 - val_accuracy: 0.6800\n",
            "Epoch 89/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.4926 - accuracy: 0.8381\n",
            "Epoch 89: accuracy did not improve from 0.85938\n",
            "7/7 [==============================] - 7s 1s/step - loss: 0.4926 - accuracy: 0.8381 - val_loss: 1.0399 - val_accuracy: 0.7400\n",
            "Epoch 90/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.4923 - accuracy: 0.8300\n",
            "Epoch 90: accuracy did not improve from 0.85938\n",
            "7/7 [==============================] - 6s 912ms/step - loss: 0.4923 - accuracy: 0.8300 - val_loss: 1.2880 - val_accuracy: 0.6612\n",
            "Epoch 91/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.5105 - accuracy: 0.8525\n",
            "Epoch 91: accuracy did not improve from 0.85938\n",
            "7/7 [==============================] - 6s 827ms/step - loss: 0.5105 - accuracy: 0.8525 - val_loss: 0.7244 - val_accuracy: 0.7937\n",
            "Epoch 92/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.4430 - accuracy: 0.8575\n",
            "Epoch 92: accuracy did not improve from 0.85938\n",
            "7/7 [==============================] - 6s 844ms/step - loss: 0.4430 - accuracy: 0.8575 - val_loss: 0.9146 - val_accuracy: 0.7613\n",
            "Epoch 93/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.4433 - accuracy: 0.8581\n",
            "Epoch 93: accuracy did not improve from 0.85938\n",
            "7/7 [==============================] - 6s 902ms/step - loss: 0.4433 - accuracy: 0.8581 - val_loss: 0.6980 - val_accuracy: 0.7650\n",
            "Epoch 94/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.4611 - accuracy: 0.8500\n",
            "Epoch 94: accuracy did not improve from 0.85938\n",
            "7/7 [==============================] - 6s 842ms/step - loss: 0.4611 - accuracy: 0.8500 - val_loss: 1.6120 - val_accuracy: 0.6875\n",
            "Epoch 95/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.4702 - accuracy: 0.8506\n",
            "Epoch 95: accuracy did not improve from 0.85938\n",
            "7/7 [==============================] - 6s 845ms/step - loss: 0.4702 - accuracy: 0.8506 - val_loss: 1.1383 - val_accuracy: 0.7337\n",
            "Epoch 96/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.4615 - accuracy: 0.8650\n",
            "Epoch 96: accuracy improved from 0.85938 to 0.86500, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 7s 995ms/step - loss: 0.4615 - accuracy: 0.8650 - val_loss: 0.5677 - val_accuracy: 0.8188\n",
            "Epoch 97/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.3762 - accuracy: 0.8788\n",
            "Epoch 97: accuracy improved from 0.86500 to 0.87875, saving model to best-model.hdf5\n",
            "7/7 [==============================] - 7s 945ms/step - loss: 0.3762 - accuracy: 0.8788 - val_loss: 0.7127 - val_accuracy: 0.7800\n",
            "Epoch 98/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.4593 - accuracy: 0.8519\n",
            "Epoch 98: accuracy did not improve from 0.87875\n",
            "7/7 [==============================] - 6s 842ms/step - loss: 0.4593 - accuracy: 0.8519 - val_loss: 0.6528 - val_accuracy: 0.7962\n",
            "Epoch 99/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.4091 - accuracy: 0.8644\n",
            "Epoch 99: accuracy did not improve from 0.87875\n",
            "7/7 [==============================] - 6s 866ms/step - loss: 0.4091 - accuracy: 0.8644 - val_loss: 0.7751 - val_accuracy: 0.7738\n",
            "Epoch 100/100\n",
            "7/7 [==============================] - ETA: 0s - loss: 0.4218 - accuracy: 0.8763\n",
            "Epoch 100: accuracy did not improve from 0.87875\n",
            "7/7 [==============================] - 6s 887ms/step - loss: 0.4218 - accuracy: 0.8763 - val_loss: 0.6394 - val_accuracy: 0.8037\n"
          ]
        }
      ],
      "source": [
        "history = model.fit(x=Xtrain, y=ytrain, batch_size=256, epochs=100, callbacks=callbacks_list, validation_data=(Xval, yval))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "cxi_gFiH4-ul"
      },
      "source": [
        "**Validation**"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 36,
      "metadata": {
        "id": "q61Lrn7G5BEV"
      },
      "outputs": [],
      "source": [
        "# load best weights\n",
        "model.load_weights(\"best-model.hdf5\")\n",
        "model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 19,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ACe3KjI-4b6t",
        "outputId": "9b5402f2-0b7f-46e9-985f-30f0176c038c"
      },
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(69919, 7, 7, 274)"
            ]
          },
          "metadata": {},
          "execution_count": 19
        }
      ],
      "source": [
        "#Xtest = Xtest.reshape(-1, 11, 11, K, 1)\n",
        "Xtest.shape"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 37,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "SuW3_rSu5Lu1",
        "outputId": "7d053ba8-7523-4a64-e3c4-230df067ea56"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "              precision    recall  f1-score   support\n",
            "\n",
            "           0       0.82      0.71      0.76      5000\n",
            "           1       0.71      0.67      0.69      5000\n",
            "           2       0.83      0.83      0.83      5000\n",
            "           3       0.92      0.98      0.95      5000\n",
            "           4       0.77      0.97      0.86      1050\n",
            "           5       0.63      0.46      0.53      4354\n",
            "           6       0.73      0.60      0.66      5000\n",
            "           7       0.74      0.70      0.72      5000\n",
            "           8       0.67      0.62      0.65      5000\n",
            "           9       0.99      0.83      0.90      5000\n",
            "          10       0.71      0.98      0.83      5000\n",
            "          11       0.32      0.97      0.48      3529\n",
            "          12       0.62      0.30      0.41      5000\n",
            "          13       0.97      0.52      0.67      5000\n",
            "          14       0.57      0.97      0.72       986\n",
            "          15       1.00      0.80      0.89      5000\n",
            "\n",
            "    accuracy                           0.72     69919\n",
            "   macro avg       0.75      0.74      0.72     69919\n",
            "weighted avg       0.77      0.72      0.72     69919\n",
            "\n"
          ]
        }
      ],
      "source": [
        "Y_pred_test = model.predict(Xtest)\n",
        "y_pred_test = np.argmax(Y_pred_test, axis=1)\n",
        "\n",
        "classification = classification_report(np.argmax(ytest, axis=1), y_pred_test)\n",
        "print(classification)"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "def AA_andEachClassAccuracy(confusion_matrix):\n",
        "    counter = confusion_matrix.shape[0]\n",
        "    list_diag = np.diag(confusion_matrix)\n",
        "    list_raw_sum = np.sum(confusion_matrix, axis=1)\n",
        "    each_acc = np.nan_to_num(truediv(list_diag, list_raw_sum))\n",
        "    average_acc = np.mean(each_acc)\n",
        "    return each_acc, average_acc\n",
        "def reports (X_test,y_test,name):\n",
        "    #start = time.time()\n",
        "    Y_pred = model.predict(X_test)\n",
        "    y_pred = np.argmax(Y_pred, axis=1)\n",
        "    #end = time.time()\n",
        "    #print(end - start)\n",
        "    if name == 'IP':\n",
        "        target_names = ['Alfalfa', 'Corn-notill', 'Corn-mintill', 'Corn'\n",
        "                        ,'Grass-pasture', 'Grass-trees', 'Grass-pasture-mowed', \n",
        "                        'Hay-windrowed', 'Oats', 'Soybean-notill', 'Soybean-mintill',\n",
        "                        'Soybean-clean', 'Wheat', 'Woods', 'Buildings-Grass-Trees-Drives',\n",
        "                        'Stone-Steel-Towers']\n",
        "    elif name == 'SA':\n",
        "        target_names = ['Brocoli_green_weeds_1','Brocoli_green_weeds_2','Fallow','Fallow_rough_plow','Fallow_smooth',\n",
        "                        'Stubble','Celery','Grapes_untrained','Soil_vinyard_develop','Corn_senesced_green_weeds',\n",
        "                        'Lettuce_romaine_4wk','Lettuce_romaine_5wk','Lettuce_romaine_6wk','Lettuce_romaine_7wk',\n",
        "                        'Vinyard_untrained','Vinyard_vertical_trellis']\n",
        "    elif name == 'PU':\n",
        "        target_names = ['Asphalt','Meadows','Gravel','Trees', 'Painted metal sheets','Bare Soil','Bitumen',\n",
        "                        'Self-Blocking Bricks','Shadows']\n",
        "    elif name == 'HC':\n",
        "        target_names = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16']\n",
        "    classification = classification_report(np.argmax(y_test, axis=1), y_pred, target_names=target_names)\n",
        "    oa = accuracy_score(np.argmax(y_test, axis=1), y_pred)\n",
        "    confusion = confusion_matrix(np.argmax(y_test, axis=1), y_pred)\n",
        "    each_acc, aa = AA_andEachClassAccuracy(confusion)\n",
        "    kappa = cohen_kappa_score(np.argmax(y_test, axis=1), y_pred)\n",
        "    score = model.evaluate(X_test, y_test, batch_size=32)\n",
        "    Test_Loss =  score[0]*100\n",
        "    Test_accuracy = score[1]*100\n",
        "    \n",
        "    return classification, confusion, Test_Loss, Test_accuracy, oa*100, each_acc*100, aa*100, kappa*100"
      ],
      "metadata": {
        "id": "i4Mxc1t9tz28"
      },
      "execution_count": 38,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "classification, confusion, Test_loss, Test_accuracy, oa, each_acc, aa, kappa = reports(Xtest,ytest,dataset)\n",
        "classification = str(classification)\n",
        "confusion = str(confusion)\n",
        "file_name = \"classification_report.txt\"\n",
        "\n",
        "with open(file_name, 'w') as x_file:\n",
        "    x_file.write('{} Test loss (%)'.format(Test_loss))\n",
        "    x_file.write('\\n')\n",
        "    x_file.write('{} Test accuracy (%)'.format(Test_accuracy))\n",
        "    x_file.write('\\n')\n",
        "    x_file.write('\\n')\n",
        "    x_file.write('{} Kappa accuracy (%)'.format(kappa))\n",
        "    x_file.write('\\n')\n",
        "    x_file.write('{} Overall accuracy (%)'.format(oa))\n",
        "    x_file.write('\\n')\n",
        "    x_file.write('{} Average accuracy (%)'.format(aa))\n",
        "    x_file.write('\\n')\n",
        "    x_file.write('\\n')\n",
        "    x_file.write('{}'.format(classification))\n",
        "    x_file.write('\\n')\n",
        "    x_file.write('{}'.format(confusion))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ILskgBa8u5s0",
        "outputId": "340094c1-bc89-48fb-a427-36df623bd267"
      },
      "execution_count": 39,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "2185/2185 [==============================] - 50s 23ms/step - loss: 1.0034 - accuracy: 0.7168\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        ""
      ],
      "metadata": {
        "id": "GBgjSP6VD7LB"
      },
      "execution_count": null,
      "outputs": []
    }
  ],
  "metadata": {
    "colab": {
      "collapsed_sections": [],
      "machine_shape": "hm",
      "name": "“Increase RAM Reference Notes By Techhawa .ipynb”的副本",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}